import env from '@/assets/env'
const markMapId = 1

function toJson(json) {
  json = json || '';
  json = json.replace(/“/g, '"').replace(/”/g, '"');
  return JSON.parse(json);
}

function getNumber(num) {
  let n = parseFloat(num);
  if (Number.isNaN(n)) n = null;
  return n;
}

function getCoordinate(marker) {
  return JSON.stringify({
    latitude: marker.latitude,
    longitude: marker.longitude,
    rotate: marker.rotate || 0
  })
}

function getIconSize(marker) {
  return JSON.stringify({
    width: marker.iconWidth,
    height: marker.iconHeight,
    opacity: marker.iconOpacity
  })
}

function getZoom(marker) {
  return JSON.stringify({
    min: marker.minzoom,
    max: marker.maxzoom
  })
}

export default {
  getMarkMapId() {
    return markMapId
  },
  getMarker(feature) {
    const coordinate = toJson(feature.coordinate)
    const iconSize = toJson(feature.iconSize)
    // const zoom = JSON.parse(feature.zoom)
    let marker = {
      ...feature,
      latitude: getNumber(coordinate.latitude),
      longitude: getNumber(coordinate.longitude),
      rotate: getNumber(coordinate.rotate) || 0,
      iconWidth: iconSize.width,
      iconHeight: iconSize.height,
      iconOpacity: iconSize.opacity
    }
    return marker
  },
  getFeature(marker) {
    let feature = {
      id: marker.id,
      landmark_id: marker.landmark_id,
      landmarkMark_type: marker.landmarkMark_type,
      coordinate: getCoordinate(marker),
      iconSize: getIconSize(marker),
      landmarkIcon_id: marker.landmarkIcon_id,
      landmarkMark_id: marker.landmarkMark_id,
      landmarkMap_id: marker.landmarkMap_id,
      minzoom: marker.minzoom,
      maxzoom: marker.maxzoom,
      onclick: marker.onclick,
      title: marker.title,
      zoom: getZoom(marker)
    }
    return feature;
  },
  hasPoint(marker) {
    return marker.latitude != null && marker.longitude != null;
  },
  // 左边的标注转地图marker
  dataToMarkerOption(data, icon, tab, longitude = 0, latitude = 0, width = 80, height = 60, minzoom = 1, maxzoom = 25) {
    let marker = {
      id: new Date().getTime(),
      longitude,
      latitude,
      width,
      height,
      minzoom,
      maxzoom,
      imageUrl: env.FILE_URL + icon.normal,
      draggable: true,
      _edit: true,
      onclick: '',
      title: data.title,
      iconWidth: width,
      iconHeight: height,
      iconOpacity: 1,
      landmarkMark_type: tab.id, // 所属类别id 1=导航语音
      landmarkMap_id: this.getMarkMapId(), // 暂时不知道放什么
      landmark_id: this.getMarkMapId(), // 当前永远等于1 将来url外部传参
      landmarkIcon_id: icon.id, // 图标样式对应id
      landmarkMark_id: data.dataID, // landmarkDatas标注接口中对应标注的dataID字段
    }
    return marker;
  }
}
